package com.action;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.annotation.Resource;

import com.model.User;
import com.service.AdminService;
import com.service.UserService;
import com.util.BaseAction;
import com.util.Pagination;

public class AdminAction extends BaseAction {
    public void setName(String name) {
        this.name = name;
    }

    public void setPagesize(Integer pagesize) {
        this.pagesize = pagesize;
    }

    public void setCurrentpage(Integer currentpage) {
        this.currentpage = currentpage;
    }

    private static final long serialVersionUID = -7001070669005326557L;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFlag() {
        return flag;
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public AdminService getAdminservice() {
        return adminservice;
    }

    public void setAdminservice(AdminService adminservice) {
        this.adminservice = adminservice;
    }

    public List<?> getList() {
        return list;
    }

    public void setList(List<?> list) {
        this.list = list;
    }

    public Pagination getPaperPage() {
        return paperPage;
    }

    public void setPaperPage(Pagination paperPage) {
        this.paperPage = paperPage;
    }

    @Resource(name = "AdminService")
    private AdminService adminservice;
    @Resource(name = "UserService")
    private UserService userService;
    private List<?> list;
    private Pagination paperPage;
    private User user;
    private String flag;
    int id;
    private Integer currentpage;
    private Integer pagesize;
    private String name;

    public String stuList() {
        currentpage = (currentpage == null ? 1 : currentpage);
        pagesize = (pagesize == null ? 10 : pagesize);
        if (paperPage == null) {
            paperPage = new Pagination(1, 10);
        }
        if (currentpage < 0) {
            currentpage = 1;
        }
        if (currentpage > paperPage.getTotalpagecount() && paperPage.getTotalpagecount() != 0) {
            currentpage = (int) paperPage.getTotalpagecount();
        }
        paperPage.setCurrentpage(currentpage);
        paperPage.setPagesize(pagesize);
        if (flag != null && flag.equals("teacher")) {
            paperPage = adminservice.stuList(paperPage, flag);

        } else {
            flag = "stu";
            paperPage = adminservice.stuList(paperPage, flag);
        }
        session.put("role", flag);
        return "stulist";
    }

    public String editUser() {
		user = new User();
		if (null != name) {
			String displayname = null;
			try {
				displayname = new String(name.getBytes("ISO-8859-1"), "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			user.setUsername(displayname);
			list = userService.findByExample(user);
			user = (User) list.get(0);
		}else{
			user = userService.findById(id);
		}
		return "editUser";
	}

    public String addTeacher() {
        userService.save(user);
        return "addTeacherok";
    }

    public String delUser() {
        userService.deleteById(id);
        return "delUser";
    }

}
